package com.novabluetooth.services.servicedevices;

import java.util.UUID;

import com.novabluetooth.services.TJBluetoothLeService;

import android.bluetooth.BluetoothGattService;
import android.util.Log;

/**
 * 捷美瑞血压计
 * 
 * @author nova
 *
 */
public class XueYaB61TJMRService extends TJBluetoothLeService {
	public final static UUID serviceXueYaUUID = UUID.fromString("000018f0-0000-1000-8000-00805f9b34fb");
	public final static UUID writeCharactXueYaUUID = UUID.fromString("00002af1-0000-1000-8000-00805f9b34fb");
	public final static UUID readCharactXueYaUUID = UUID.fromString("00002af0-0000-1000-8000-00805f9b34fb");
	private String BPM_readData = "0240dc01a13c";// 开始测量
	private String BPM_closeDevice = "0240dc01a23f";// 结束测量
	private String BPM_closeSound = "0240dc01a33e";// 取消声音
	private static String[] binaryArray = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000",
			"1001", "1010", "1011", "1100", "1101", "1110", "1111" };
	@Override
	public void servicesDiscovered() {
		super.servicesDiscovered();
		BluetoothGattService gattService = null;
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
			gattService = mBluetoothGatt.getService(serviceXueYaUUID);
			readGattCharact = gattService.getCharacteristic(readCharactXueYaUUID);
			writeGattCharact = gattService.getCharacteristic(writeCharactXueYaUUID);
			mBluetoothGatt.setCharacteristicNotification(readGattCharact, true);
			ifSuccess = writeDataToDevice(BPM_readData);// 开始测量
			writeDataToDevice(BPM_closeSound);// 消除声音
		}
	}

	@Override
	public boolean isRightData(byte[] buffer) {
		super.isRightData(buffer);
		int readSize_xy = buffer.length;
		Log.e("BluetoothLeService", "血压计接收到的数据信息为：" + byteArray2HexString(buffer, readSize_xy));
		ifSuccess = false;
		if (readSize_xy <= 0) {
			Log.e("BluetoothLeService", "数据为空");
			ifSuccess = false;
		} else if (readSize_xy < 7) {
			Log.e("BluetoothLeService", "BTThread Data is Short");
			ifSuccess = false;
		} else if (readSize_xy == 7) {
			String isStart = byteArray2HexString(new byte[] { buffer[0] }, new byte[] { buffer[0] }.length);
			Log.e("BluetoothLeService", "isStart === " + isStart);
			if ("02".equals(isStart)) {
				// 无语音
				// isStamping = true;
				// 压力值
				int yali = (buffer[4] & 0xFF) + (buffer[5] & 0xFF);
				checkData = yali + "";
				ifSuccess = false;
			} else if ("FF".equals(isStart)) {
				int gaoya = (buffer[3] & 0xFF);
				// 低压
				int diya = (buffer[4] & 0xFF);
				// 心率
				int xinlv = (buffer[5] & 0xFF);
				if (xinlv == 4) {
					checkData = "测量失败，请重新测量！";
				} else {

					checkData = gaoya + ";" + diya + ";" + xinlv;
				}
				ifSuccess = true;
			}
			Log.e("BluetoothLeService---FF----", "readSize == FF出结果" + checkData);
			// 压力值不需要传输
			// ifSuccess = true;
		} else if (readSize_xy == 8) {
			int dianliang = (buffer[6] & 0xFF);

			Log.e("BluetoothLeService", "电量:  " + dianliang);
			if (dianliang <= 10) {
				checkData = "臂式血压计电量不足，请及时充电";
//				GlobalConts.showTextDialog(getApplicationContext(), "臂式血压计电量不足，请及时充电");
				// GlobalConts
				// .showToast(getApplicationContext(), "电量不足，请及时充电");
			}
			ifSuccess = true;
		} else if (readSize_xy == 17) {
			String isCehggongString = bytes2BinaryStr(new byte[] { buffer[4] });
			System.out.println(isCehggongString);
			if ("0".equals(isCehggongString.substring(2, 3))) {
				// 高压
				int gaoya = (buffer[5] & 0xFF) + (buffer[6] & 0xFF);
				// 低压
				int diya = (buffer[7] & 0xFF) + (buffer[8] & 0xFF);
				// 心率
				int xinlv = (buffer[11] & 0xFF) + (buffer[12] & 0xFF);
				checkData = gaoya + ";" + diya + ";" + xinlv;
				Log.e("BluetoothLeService", "readSize == 17出结果" + checkData);
			} else {
				checkData = "测量失败，请重新测量！";
			}
			ifSuccess = true;
			writeDataToDevice(BPM_closeDevice);
		} 
		return ifSuccess;

	}

	@Override
	public void closeDevice() {
	}
	/**
	 * 
	 * @param str
	 * @return 转换为二进制字符串
	 */
	public static String bytes2BinaryStr(byte[] bArray) {

		String outStr = "";
		int pos = 0;
		for (byte b : bArray) {
			// 高四位
			pos = (b & 0xF0) >> 4;
			outStr += binaryArray[pos];
			// 低四位
			pos = b & 0x0F;
			outStr += binaryArray[pos];
		}
		return outStr;

	}
}
